#include "wds_bullet_manager.h"
#include "wds_bullet_collection.h"
#include "wds_bullet_rocket.h"
#include "wds_rocket_seeker.h"
#include "wds_sounds.h"
#include "wds_bullet_sfx.h"

#include "num/rand.h"
#include "math/facing.h"
#include "util/log.h"				// log
#include "util/common_macros.h"		// macros
#include "util/mmgr.h"		    
#include <cassert>

WDSBulletManager::WDSBulletManager()
{
	
}
	
WDSBulletManager::~WDSBulletManager()
{	

	for(unsigned int i = 0; i < WDSBullet::BULLET_TYPE_C; i++)
	{
		UTIL_SAFE_DELETE(m_pBulletCollections[i]);
	}
	
}

WDSBulletManager&		WDSBulletManager::Instance()
{
	static WDSBulletManager Inst;
	return Inst;
}


void	WDSBulletManager::Render(const math::Vec2& Offset,float Scale)
{
	for(unsigned int i = 0; i < WDSBullet::BULLET_TYPE_C; i++)
	{
		WDSBulletCollection* pBulletCollection = m_pBulletCollections[i];
		if(pBulletCollection)
			pBulletCollection->Render(Offset,Scale); 
	}
}

void	WDSBulletManager::RenderDebug(const math::Vec2& Offset,float Scale)
{
	for(unsigned int i = 0; i < WDSBullet::BULLET_TYPE_C; i++)
	{
		WDSBulletCollection* pBulletCollection = m_pBulletCollections[i];
		if(pBulletCollection)
			pBulletCollection->RenderDebug(Offset,Scale); 
	}
}

void	WDSBulletManager::Tick(float dt, const math::Vec2& Offset)
{
	for(unsigned int i = 0; i < WDSBullet::BULLET_TYPE_C; i++)
	{
		WDSBulletCollection* pBulletCollection = m_pBulletCollections[i];
		if(pBulletCollection)
			pBulletCollection->Tick(dt, m_Skill.m_BulletSpeed, Offset); 
	}
}

void	WDSBulletManager::Clear()
{
	for(unsigned int i = 0; i < WDSBullet::BULLET_TYPE_C; i++)
	{
		WDSBulletCollection* pBulletCollection = m_pBulletCollections[i];
		if(pBulletCollection)
			pBulletCollection->Clear(); 
	}
	
}

void	WDSBulletManager::DieAll()
{
	for(unsigned int i = 0; i < WDSBullet::BULLET_TYPE_C; i++)
	{
		WDSBulletCollection* pBulletCollection= m_pBulletCollections[i];
		if(pBulletCollection)
			pBulletCollection->DieAll(); 
	}
}



WDSBullet*		WDSBulletManager::Shoot(		WDSBullet::BULLET_TYPES Type,
												WDSLifeObject* pShooter,
												const math::Vec2& ShootPos,														
												float Degree,																			
												int SoundSFXIndex
												)
{
	if(Type < 0)
		return NULL;

	if( Type >= WDSBullet::BULLET_TYPE_C)
		return NULL;

	WDSBullet* pBullet = NULL;

	WDSBulletCollection* pBulletCollection= m_pBulletCollections[Type];
	if(!pBulletCollection)
	{
		m_pBulletCollections[Type] = new WDSBulletCollection(Type);
		pBulletCollection = m_pBulletCollections[Type];

	}
	pBullet= pBulletCollection->Shoot(pShooter, ShootPos, Degree, SoundSFXIndex);
	if(pBullet)
		pBullet->SetDamagePercentage( m_Skill.m_SilverBullet);

	return pBullet;

}


WDSBulletManager::Skill::Skill():	m_SilverBullet(1.0f),		// DONE		
									m_BulletSpeed(1.0f),		// DONE								
									m_IONGunMaster(2.0f)		// DONE
{
	
}